package pl.mcnova.model.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import pl.mcnova.model.HibernateSession;
import pl.mcnova.model.entity.Contractor;

public class ContractorDAO {

	private SessionFactory sessionFactory;
	private Session session;

	public ContractorDAO() {

		sessionFactory = HibernateSession.getInstance();

	}

	public void save(Contractor contractor) {
		session = sessionFactory.openSession();
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			session.save(contractor);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				try {
					transaction.rollback();

				} catch (HibernateException he) {

				}
			}
		} finally {
			try {
				session.close();
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	public void update(Contractor contractor) {
		session = sessionFactory.openSession();
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			session.update(contractor);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				try {
					transaction.rollback();

				} catch (HibernateException he) {

				}
			}
		} finally {
			try {
				session.close();
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	public void delete(Contractor contractor) {
		session = sessionFactory.openSession();
		Transaction transaction = null;

		try {
			transaction = session.beginTransaction();
			session.delete(contractor);
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null) {
				try {
					transaction.rollback();

				} catch (HibernateException he) {

				}
			}
		} finally {
			try {
				session.close();
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	@SuppressWarnings("finally")
	public Contractor get(Integer contractorID) {
		session = sessionFactory.openSession();
		Contractor contractor = null;

		try {

			contractor = (Contractor) session.get(Contractor.class, contractorID);

		} catch (Exception e) {

		} finally {
			try {
				session.close();
				return contractor;
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	public List<Contractor> getAll() {
		session = sessionFactory.openSession();

		List<Contractor> contractor = null;

		try {

			Criteria criteria = session.createCriteria(Contractor.class);
			criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			contractor = criteria.list();

			// Query query =
			// session.createQuery("from contractors where id = 1");//select
			// distinct c from contractors c left join fetch c.devices where
			// type = :typ");
			// query.setString("typ", Tabs.architects);
			// contractor = query.list();

		} catch (Exception e) {

		} finally {
			try {
				session.close();
				return contractor;
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	public List<Contractor> getAllOfType(String type) {
		session = sessionFactory.openSession();

		List<Contractor> contractor = null;

		try {

			Criteria criteria = session.createCriteria(Contractor.class);
			criteria.add(Restrictions.like("contractorType", type));
			criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			contractor = criteria.list();

			// Query query =
			// session.createQuery("from contractors where id = 1");//select
			// distinct c from contractors c left join fetch c.devices where
			// type = :typ");
			// query.setString("typ", Tabs.architects);
			// contractor = query.list();

		} catch (Exception e) {

		} finally {
			try {
				session.close();
				return contractor;
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

	public List<Contractor> getAll(String city, String street, String postalCode, String houseNumber) {
		session = sessionFactory.openSession();

		List<Contractor> contractor = null;

		try {

			Criteria criteria = session.createCriteria(Contractor.class);
			criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
			criteria.add(Restrictions.like("city", city));
			criteria.add(Restrictions.like("street", street));
			criteria.add(Restrictions.like("postalCode", postalCode));
			criteria.add(Restrictions.like("houseNumber", houseNumber));

			contractor = criteria.list();

		} catch (Exception e) {

		} finally {
			try {
				session.close();
				return contractor;
			} catch (HibernateException he) {
				throw he;
			}
		}
	}

}
